package com.protobuf;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.protobuf.model.UserProto;

/**
 * This example demonstrates serving up REST payloads encoded using
 * <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
 */
@SpringBootApplication
public class ProtobufTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProtobufTestApplication.class, args);
    }

    @Bean
	RestTemplate restTemplate() {
		RestTemplate rt = new RestTemplate();
		rt.getMessageConverters().add(new ProtobufHttpMessageConverter());
		return rt;
	}
}

@RestController
class UserRestController {

	@Autowired
	private RestTemplate restTemplate;
	
	
	
    @RequestMapping("/user/{id}")
    UserProto.User user(@PathVariable Integer id) {
    	
    	UserProto.User obj = this.restTemplate.getForObject("http://127.0.0.1:8080/user/" + id,
    			UserProto.User.class);
        return obj;
    }
    
    @RequestMapping("/user2/{id}")
    String user2(@PathVariable Integer id) {
    	
    	UserProto.User obj = this.restTemplate.getForObject("http://127.0.0.1:8080/user/" + id,
    			UserProto.User.class);
        return obj.toString();
    }
    
    @RequestMapping("/map/{id}")
    Map<String,String> map(@PathVariable Integer id) {
    	Map m = this.restTemplate.getForObject("http://127.0.0.1:8080/map/" + id, Map.class);
		return m;
    }
    
    
    
}